package com.example.web.websystem.service;

import com.example.web.websystem.dao.SysGroupDao;
import com.example.web.websystem.dao.model.SysGroupDO;
import com.example.web.websystem.dao.model.SysRoleDO;
import com.example.web.websystem.dao.model.SysUserDO;
import com.example.web.websystem.response.BusinessError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by YangGang on 2021/4/6 13:42。
 */
@Service
public class SysGroupService {
    @Autowired
    private SysGroupDao sysGroupDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 查询-所有组织
     *
     * @return
     */
    public List<SysGroupDO> listAllSysGroup() {
        return sysGroupDao.listAll();
    }

    /**
     * 添加-组织（包括添加组织管理员）
     *
     * @param sysGroup       待添加组织对象
     * @param groupAdminUser 待添加组织管理员对象
     * @return 新组织id
     */
    @Transactional
    public Long addSysGroup(SysGroupDO sysGroup, SysUserDO groupAdminUser) {
        try {
            //添加组织
            sysGroup.setGroupId(null);
            sysGroupDao.insertSelective(sysGroup);
            //添加组织第一个角色（组织管理员）
            SysRoleDO sysRole = new SysRoleDO();
            sysRole.setRoleName(sysGroup.getGroupName() + "-管理员");
            sysRole.setGroupId(sysGroup.getGroupId());
            sysRole.setRolePaths("*");
            sysRoleService.addSysRole(sysRole);
            //添加组织管理员用户
            if (sysGroup.getGroupAdminUserId() != null && groupAdminUser != null && groupAdminUser.getUserId() != null)
                throw new BusinessError("组织管理员设置失败");
            if (groupAdminUser != null && groupAdminUser.getUserId() == null) {
                groupAdminUser.setGroupId(sysGroup.getGroupId());
                groupAdminUser.setRoleIds(sysRole.getRoleId().toString());
                sysUserService.addSysUser(groupAdminUser);
                sysGroupDao.updateGroupAdminUserId(sysGroup.getGroupId(), groupAdminUser.getUserId());
            } else if (sysGroup.getGroupAdminUserId() != null) {
                String roleIds = sysUserService.getSysUserByUserId(sysGroup.getGroupAdminUserId()).getRoleIds();
                if (roleIds != null || !roleIds.isEmpty()) {
                    roleIds += "," + sysRole.getRoleId();
                }
                sysUserService.updateSysUserRoleIds(sysGroup.getGroupAdminUserId(), roleIds);
            }
        } catch (BusinessError e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessError("添加组织失败");
        }
        return sysGroup.getGroupId();
    }

    @Transactional
    public int updateSysGroup(SysGroupDO sysGroup) {
        int up = sysGroupDao.updateByPrimaryKeySelective(sysGroup);
        if (up != 1) throw new BusinessError("更新组织信息失败");
        return up;
    }

    /**
     * 删除-组织：逻辑删除
     *
     * @param groupId
     * @return
     */
    @Transactional
    public int deleteSysGroup(long groupId) {
        SysGroupDO sysGroup = new SysGroupDO();
        sysGroup.setGroupId(groupId);
        sysGroup.setIsDeleted(true);
        int up = sysGroupDao.updateByPrimaryKeySelective(sysGroup);
        if (up != 1) throw new BusinessError("删除组织失败");
        return up;
    }

    /**
     * 修改-组织状态为正常
     *
     * @param groupId
     * @return
     */
    @Transactional
    public int restoreSysGroup(long groupId) {
        SysGroupDO sysGroup = new SysGroupDO();
        sysGroup.setGroupId(groupId);
        sysGroup.setIsDeleted(false);
        int up = sysGroupDao.updateByPrimaryKeySelective(sysGroup);
        if (up != 1) throw new BusinessError("恢复组织失败");
        return up;
    }


    /**
     * 添加-组织管理员
     *
     * @param sysUser
     * @return
     */
    @Transactional
    public int addSysGroupAdminUser(SysUserDO sysUser) {
        //添加user
        sysUserService.addSysUser(sysUser);
        //设置为组织领导人
        int up = sysGroupDao.updateGroupAdminUserId(sysUser.getGroupId(), sysUser.getUserId());
        if (up != 1) throw new BusinessError("添加组织管理员失败");
        return up;
    }


    /**
     * 更新-组织的路由
     *
     * @param groupId    组织的ID
     * @param groupPaths 组织的路由path（即组织的菜单，多个用逗号隔开）
     * @return
     */
    @Transactional
    public int updateSysGroupGroupPaths(long groupId, String groupPaths) {
        int up = sysGroupDao.updateGroupPaths(groupId, groupPaths);
        return up;
    }

    public SysGroupDO getSysGroup(Long groupId) {
        return sysGroupDao.selectByPrimaryKey(groupId);
    }

}
